package com.ttdevs.app;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Process;

import com.android.volley.VolleyLog;
import com.ttdevs.util.Utils;

interface IBitmapManager {

	public boolean saveBitmap(String key, Bitmap bitmap);

	public Bitmap loadBitmap(String key);
}

public class BitmapSDCardCache extends Thread implements IBitmapManager {

	private final static String ROOTPATHDIR = Utils.getImageCacheDir();
	private final static int QUEUESIZE = 9;
	private BlockingQueue<BitmapEntry> mQueue;
	private Map<String, File> fileMap;

	private volatile boolean mQuit = false;

	public BitmapSDCardCache() {
		mQueue = new ArrayBlockingQueue<BitmapSDCardCache.BitmapEntry>(QUEUESIZE);
		fileMap = new HashMap<String, File>();
		initImageDir();
		initImageList();
	}

	private void initImageDir() {
		if (Utils.isExternalStorageWritable()) {
			File pathFile = new File(ROOTPATHDIR);
			if (!pathFile.exists()) {
				pathFile.mkdirs();
			}
		} else {
			VolleyLog.d("%s", "外部媒体未加载,初始化失败");
		}
	}

	private void initImageList() {
		if (Utils.isExternalStorageWritable()) {
			File[] files = new File(ROOTPATHDIR).listFiles();
			if (files != null) {
				for (File file : files) {
					fileMap.put(file.getName(), file);
				}
			}
		} else {
			VolleyLog.d("%s", "外部媒体未加载,初始化失败");
		}
	}

	@Override
	public boolean saveBitmap(String key, Bitmap bitmap) {
		try {
			mQueue.put(new BitmapEntry(key, bitmap));
		} catch (InterruptedException e) {
			VolleyLog.d("%s", e.getMessage());
			return false;
		}
		return true;
	}

	@Override
	public Bitmap loadBitmap(String key) {
		Bitmap bmp = null;
		try {
			bmp = BitmapFactory.decodeFile(ROOTPATHDIR + Utils.getVolleyRuleFileName(key));
		} catch (Exception e) {
			VolleyLog.d("%s", e.getMessage());
		}
		return bmp;
	}

	public void quit() {
		mQuit = true;
		interrupt();
	}

	@Override
	public void run() {
		Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
		mQueue.clear();

		while (true) {
			try {
				final BitmapEntry entry = mQueue.take();
				if (Utils.isExternalStorageWritable()) {
					if (fileMap.containsKey(entry.key)) {
						continue;
					} else if (Utils.saveBitmap2file(ROOTPATHDIR, entry.getKey(), entry.getBitmap())) {
						File file = new File(ROOTPATHDIR, entry.getKey());
						fileMap.put(file.getName(), file);
						continue;
					} else {
						VolleyLog.d("%s", "保存文件失败");
					}
				} else {
					VolleyLog.d("%s", "外部媒体未加载");
				}

			} catch (InterruptedException e) {
				if (mQuit) {
					return;
				}
				continue;
			}
		}
	}

	protected class BitmapEntry {
		private String key;
		private Bitmap bitmap;

		public BitmapEntry(String key, Bitmap bitmap) {
			this.key = key;
			this.bitmap = bitmap;
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public Bitmap getBitmap() {
			return bitmap;
		}

		public void setBitmap(Bitmap bitmap) {
			this.bitmap = bitmap;
		}
	}
}
